
import type { Plugin } from 'vite';
import * as parser from '@babel/parser'
import traverse from '@babel/traverse'
import generate from '@babel/generator'
import * as t from '@babel/types'
export default function removeDevCodePlugin(options?:any): Plugin {
    return {
        name: 'vite-plugin-remove-dev-code',
        enforce: 'pre', // 提前运行

        transform(code, id) {
            // id 是文件名，只处理 vue/js/ts 文件
            // console.log(id)
            if (!id.match(/\.(vue|ts|js)$/)) return;
            if (process.env.NODE_ENV === 'development') {
                console.log(code)
                if(options.autoImportVue){
                    code = `import {ref,reactive,computed,watch} from 'vue';`+code
                }
                if(options.dropConsole){
                    code = code.replace(/console\.log\(.*\)/g,'')
                }
                console.log(options.dropDebugger)
                if(options.dropDebugger){
                    code = code.replace(/debugger/g,'')
                }
                if(options.autoImportLodash){
                    code = `import _ from 'lodash';`+code
                }
                console.log(code)
            }

            return code;
        }
    }
}